package com.deer.wms.instock.service.enter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.base.system.model.Item.ItemInfo;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.instock.model.erpType.ErpType;
import com.deer.wms.instock.service.enter.EnterErp;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PushErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: xu
 * @Date: 2022/06/20/11:19
 * @Description: null新增，10其他入库单
 */
@Component

public class Else  implements EnterErp {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private PushErpService pushErpService;
    @Autowired
    private ItemInfoService itemInfoService;

    @Override
    public ErpType gainType() {
        return new ErpType(null, 10);
    }

    @Override
    public Map<String, String> EnterBeanAndPush(AsnMaster asnMaster, List<AsnDetail> asnDetails, BaseAssist enterAssist) {




        OrderToErpBO orderToErpBO = new OrderToErpBO();
        List<OrderDetailBO> details = new ArrayList<>();
        orderToErpBO.setBillTypeId(asnMaster.getAsnType());
        orderToErpBO.setSourceBillTypeId(asnMaster.getBillType());
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("INPUT"));
        orderToErpBO.setRob(true);
        orderToErpBO.setDeptId(asnMaster.getDeptId());
        orderToErpBO.setSupplierId(enterAssist.getSupplierId());
        orderToErpBO.setBillDate(DateUtils.getNowDateString()); //日期        orderToErpBO.setCategoryId(ObjectUtil.isNotEmpty(asnMaster.getCategoryId()) ? asnMaster.getCategoryId() : 41146); //默认其他调拨
//        orderToErpBO.setCategoryId(ObjectUtil.isNotEmpty(asnMaster.getCategoryId()) ? asnMaster.getCategoryId() : 41146); //默认其他调拨

        orderToErpBO.setCategoryId(ObjectUtil.isNotEmpty(enterAssist.getCategoryId()) ? enterAssist.getCategoryId() : 27972); //默认其他调拨

        for (AsnDetail asnDetail : asnDetails) {
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            orderDetailBO.setMtoNo(asnDetail.getMtoNo());
            orderDetailBO.setSourceBillId(asnDetail.getSourceId()); //源单id
            orderDetailBO.setSourceBillDetailRowIndex(asnDetail.getRowIndex()); //行号
            orderDetailBO.setBatchNo(asnDetail.getBatchName()); //批号
            orderDetailBO.setQty(asnDetail.getQty());
//            orderDetailBO.setAuxPropId(33469);
            ItemInfo itemInfo = itemInfoService.findBy("item_code", asnDetail.getItemCode());

            orderDetailBO.setItemId(itemInfo.getErpItemId());
            orderDetailBO.setStockId(asnDetail.getStockId());
            details.add(orderDetailBO);
        }
        orderToErpBO.setDetails(details);


        return pushErpService.CreateIMBill(orderToErpBO);
    }
}
